﻿//
// Gns_TwoCodeWnd.cpp
//
//
// Created by yjf
//
//

#include "Gns_TwoCodeWnd.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()


enum 
{
	BTN_BACK,
	BTN_ANDROID,
	BTN_IOS,
	BTN_COPY,
};


Gns_TwoCodeWnd::Gns_TwoCodeWnd()
{
   
}

Gns_TwoCodeWnd::~Gns_TwoCodeWnd()
{
    
}

Gns_TwoCodeWnd* Gns_TwoCodeWnd::Create(string csbName,MOVABLE_TYPE showType, MOVABLE_TYPE hideType)
{
	auto wnd = new Gns_TwoCodeWnd();

	if (wnd && wnd->init(csbName,showType,hideType))
	{
		wnd->autorelease();
	}
	else
	{
		CC_SAFE_DELETE(wnd);
		wnd = nullptr;
	}
	
    
	return wnd;
}

bool Gns_TwoCodeWnd::init(string csbName, MOVABLE_TYPE showType, MOVABLE_TYPE hideType)
{
	if (!PopWnd::init(csbName))
	{
		return false;
	}

	_tShowType = showType;
	_tHideType = hideType;

	m_closeBtn = static_cast<Button *>(_rootNode->getChildByName("Button_1"));
	m_closeBtn->addTouchEventListener(CC_CALLBACK_2(Gns_TwoCodeWnd::doEvent, this));
	m_closeBtn->setTag(BTN_BACK);

	return true;
}

// void Gns_TwoCodeWnd::SetNodeName(std::string CsbName)
// {
// 	m_RootNode = Loadcsb(CsbName);
// 	addChild(m_RootNode, 11);
//     
// 	GNS_PlayerMgr::GetInstance()->_NodeChildVec.pushBack(m_RootNode);
// 	auto pBackBtn = static_cast<Button*>(m_RootNode->getChildByName("Button_3"));
//     pBackBtn->setTag(BTN_BACK);
//     pBackBtn->addTouchEventListener(CC_CALLBACK_2(Gns_TwoCodeWnd::doEvent, this));
//     
// 	auto AndroidBtn = static_cast<Button*>(m_RootNode->getChildByName("AndroidBtn"));
//     AndroidBtn->setTag(BTN_ANDROID);
//     AndroidBtn->addTouchEventListener(CC_CALLBACK_2(Gns_TwoCodeWnd::doEvent, this));
//     
// 	auto IOSBtn = static_cast<Button*>(m_RootNode->getChildByName("IOSBtn"));
//     IOSBtn->setTag(BTN_IOS);
//     IOSBtn->addTouchEventListener(CC_CALLBACK_2(Gns_TwoCodeWnd::doEvent,this));
//     
// 	auto CopyBtn = static_cast<Button *>(m_RootNode->getChildByName("CopyBtn"));
//     CopyBtn->setTag(BTN_COPY);
//     CopyBtn->addTouchEventListener(CC_CALLBACK_2(Gns_TwoCodeWnd::doEvent, this));
//     
//     AndroidDownText=GNS_PlayerMgr::GetInstance()->getTwoCodeInfo();
// 	IOSDownText = GNS_PlayerMgr::GetInstance()->getTwoCodeInfo(1);
// 	DownText = static_cast<Text*>(m_RootNode->getChildByName("Text_1"));
// 	m_AndroidCode = CreateQRImage(AndroidDownText);
// 	m_iosCode = CreateQRImage(IOSDownText);
// 	DownText->setString(SetShowText((char*)AndroidDownText.c_str(), "......", 20));
// }

void Gns_TwoCodeWnd::doEvent(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) 
	{
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) 
		{
            case BTN_BACK:
            {
               
				Show(false);
                break;
            }
            case BTN_ANDROID:
            {
				if (m_iosCode != nullptr)
				{
					m_iosCode->setVisible(false);
				}
				else
				{
					DownText->setString("暂无");
				}
				if (m_AndroidCode)
				{
					m_AndroidCode->setVisible(true);
					DownText->setString(SetShowText((char*)AndroidDownText.c_str(), "......", 20));
				}else
				{
					DownText->setString("暂无");
				}
                
				break;
            }
            case BTN_IOS:
            {
				if (m_iosCode!=nullptr)
				{
					m_iosCode->setVisible(true);
					DownText->setString(SetShowText((char*)IOSDownText.c_str(), "......", 20));
				}
				else
				{
					DownText->setString("暂无");
				}
				if (m_AndroidCode)
				{
					m_AndroidCode->setVisible(false);
				}
				else
				{
					DownText->setString("暂无");
				}
				break;
            }
            case BTN_COPY:
            {
#if (CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
                bool IsSuccessful=false;
				IsSuccessful = GNS_IOSHelper::GetInstance()->GetClipboard(IOSDownText);
                if (IsSuccessful==true) {
					ShowCommonTips("复制成功!",Color3B(0,0,0),28);
                } else{
					ShowCommonTips("复制失败!",Color3B(0,0,0),28);
                }
#else if(CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID)
				{
				//	SystemMgr::getInstance()->setAndroidClipJNI(AndroidDownText.c_str());
				//	ShowCommonTips("复制成功!");
				}
                
#endif
                break;
            }
            default:
                break;
        }
    }
}

DrawNode* Gns_TwoCodeWnd::CreateQRImage(std::string QRImageName)
{
	bool bRet = m_QREncode.EncodeData(0, 0, 1, -1, (char*)QRImageName.c_str());
    if(bRet)
    {
		float nSize = m_qrSize / m_QREncode.m_nSymbleSize; // 定义好像素点的大小
        int originalSize = m_QREncode.m_nSymbleSize + (QR_MARGIN * 2);
        DrawNode *pQRNode = DrawNode::create();
        // 绘制像素点
        for (int i = 0; i < m_QREncode.m_nSymbleSize; ++i)
        {
            for (int j = 0; j < m_QREncode.m_nSymbleSize; ++j)
            {
                if (m_QREncode.m_byModuleData[i][j]==1)
                {
                    pQRNode->drawSolidRect(Vec2((i + QR_MARGIN)*nSize, (j + QR_MARGIN)*nSize), Vec2(((i + QR_MARGIN) + 1)*nSize, ((j + QR_MARGIN) + 1)*nSize), Color4F(0, 0, 0, 1));
                }
                else
                {
                    pQRNode->drawSolidRect(Vec2((i + QR_MARGIN)*nSize, (j + QR_MARGIN)*nSize), Vec2(((i + QR_MARGIN) + 1)*nSize, ((j + QR_MARGIN) + 1)*nSize), Color4F(1, 1, 1, 1));
                }
            }
        }
        // 绘制外框
        pQRNode->drawSolidRect(Vec2(0, 0), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (QR_MARGIN)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2(0, 0), Vec2((QR_MARGIN)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN)*nSize, 0), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        pQRNode->drawSolidRect(Vec2(0, (m_QREncode.m_nSymbleSize + QR_MARGIN)*nSize), Vec2((m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize, (m_QREncode.m_nSymbleSize + QR_MARGIN * 2)*nSize), Color4F(1, 1, 1, 1));
        Size winSize = Director::getInstance()->getWinSize();
        pQRNode->setPosition(Vec2((winSize.width - nSize*m_QREncode.m_nSymbleSize) / 2, winSize.height - (winSize.height - nSize*m_QREncode.m_nSymbleSize) / 2));
        pQRNode->setScaleY(-1);
		getRootNode()->addChild(pQRNode, 12);
		return pQRNode;
    }
	return 	nullptr;
}